﻿using System.Collections.Generic;
using System.Threading.Tasks;
using ECommon.Utilities;
using ENode.Commanding;
using ESTAR.Commands.Auth;
using ESTAR.Infrastructure;
using ESTAR.QueryServices.TaskQueryServices;
using ESTAR.QueryServices.UserQueryServices;
using Microsoft.AspNetCore.Mvc;

namespace User.API
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        private readonly ICommandService _commandService;
        private readonly IUserQueryServices _userQuery;
        private readonly ITaskQueryServices _taskQueryServices;

        public ValuesController(ICommandService commandService, IUserQueryServices userQuery, ITaskQueryServices taskQueryServices)
        {
            _commandService = commandService;
            _userQuery = userQuery;
            _taskQueryServices = taskQueryServices;
        }
        // GET api/values
        [HttpGet]
        public async Task<object> Get()
        {
            string pwd = PasswordHash.CreateHash("123456");
            var result = await _commandService.ExecuteAsync(new RegisterNewUserInfoCommand(ObjectId.GenerateNewStringId(),
                "测试2", pwd), CommandReturnType.EventHandled);
            if (result.Status != CommandStatus.Success)
            {
                return new { success = false, errorMsg = result.Result };
            }

            var commandResult = result;
            if (commandResult.Status == CommandStatus.Failed)
            {
                if (commandResult.ResultType == typeof(DuplicateAccountException).Name)
                {
                    return new { success = false, errorMsg = "该账号已被注册，请用其他账号注册。" };
                }
                return new { success = false, errorMsg = commandResult.Result };
            }

            //await SignInAsync(commandResult.AggregateRootId, model.AccountName, false);
            return new { success = true };
        }

        // GET api/values/5
        [HttpGet("{id}")]
        public List<string> Get(string id)
        {
            return new List<string> { _userQuery.GetUserName(id), _taskQueryServices.GetTaskName(id) };
        }

        // POST api/values
        [HttpPost]
        public void Post([FromBody] string value)
        {
        }

        // PUT api/values/5
        [HttpPut("{id}")]
        public void Put(int id, [FromBody] string value)
        {
        }

        // DELETE api/values/5
        [HttpDelete("{id}")]
        public void Delete(int id)
        {
        }
    }
}
